<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>购物车</title>
    <script src="https://cdn.staticfile.org/vue/2.7.0/vue.min.js"></script>
    <style>
      table {
        width: 100%;
        border-collapse: collapse;
      }

      th,
      td {
        padding: 8px;
        text-align: left;
        border-bottom: 1px solid #ddd;
      }

      #tables thead tr:first-child {
        background-color: aliceblue;
        color: grey;
      }

      #tables tbody tr:nth-child(odd) {
        background-color: lightgoldenrodyellow;
      }

      #tables tbody tr:hover {
        background-color: rgb(249, 234, 254);
      }
    </style>
  </head>
  <body>
    <div id="app" style="width: 1000px; height: 50px; margin: auto">
      <!-- <input type="text" v-model="search" /> -->
      <!-- <button @click="sear">搜索</button> -->
      <table id="tables">
        <thead>
          <tr>
            <th>
              <input
                type="checkbox"
                v-model="selectAll"
                @change="SelectAll"
              />全选
            </th>
            <th>商品</th>
            <th>图片</th>
            <th>单价</th>
            <th>购买数量</th>
            <th>小计</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(item, index) in shops" :key="index">
            <td><input type="checkbox" v-model="item.selected" /></td>
            <td>{{item.name}}</td>
            <td>
              <img :src="item.img" alt="" style="width: 100px; height: 80px" />
            </td>
            <td>￥{{item.price | upname}}</td>
            <td>
              <button @click="item.quanity>1?item.quanity--:0">-</button>
              <input type="text" v-model="item.quanity" style="width: 30px" />
              <button @click="item.quanity++">+</button>
            </td>
            <td>小计:{{item.price*item.quanity | upnum}}</td>
            <td>
              <button @click="del(index)">删除</button>
            </td>
          </tr>
        </tbody>
      </table>
      <div>总计：￥{{total}}</div>
    </div>
    <script>
          new Vue({
            el: "#app",
            data: {
              // total: "",
              selectAll: "",
              index: "",
              count: "",
              allnum: "",
              price: "",
              flag: "",
              quanity: 1,
              shops: [
                {
                  name: "技嘉 酷睿i5 13400F",
                  img: "https://img2.baidu.com/it/u=4081124703,2799755819&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500",
                  price: "3585.00",
                  quanity: "1",
                  flag: false,
                },
                {
                  name: "罗技(G)GPW3 狗屁王三代无线游戏鼠标",
                  img: "https://t15.baidu.com/it/u=2518822975,1192498352&fm=224&app=112&f=JPEG?w=500&h=500",
                  price: "899.00",
                  quanity: "1",
                  flag: false,
                },
                {
                  name: "迪奥DIOR魅惑变色润唇膏",
                  img: "https://img0.baidu.com/it/u=1266351986,4219037793&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500",
                  price: "380.00",
                  quanity: "1",
                  flag: false,
                },
                {
                  name: "香奈儿(Chanel)邂逅柔情淡香水",
                  img: "https://img1.baidu.com/it/u=412298005,1109635090&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500",
                  price: "1000.00",
                  quanity: "1",
                  flag: false,
                },
                {
                  name: "华硕ROG XG27UCG绝神27英寸显示器",
                  img: "https://t14.baidu.com/it/u=406458971,1390790053&fm=224&app=112&f=JPEG?w=500&h=500",
                  price: "2899.00",
                  quanity: "1",
                  flag: false,
                },
              ],
            },
            computed: {
              total() {
                let sum = 0;
                this.shops.forEach((item) => {
                  if (item.flag) {
                    sum += item.price * item.quanity;
                  }
                }); 
                return sum;
              },
              totals() {
                let totalsum = 0;
                this.shops.forEach((item) => {
                  if (item.flag) {
                    totalsum += item.price * item.quanity;
                  }
                });
                return totalsum;
              },
            },
            methods: {
              SelectAll() {
                this.shops.forEach((shop) => {
                  shop.selected = this.selectAll;
                });
              },
              del(index) {
                console.log(11111);

                if (confirm("确定删除吗？")) {
                  this.shops.splice(index, 1);
                } else {
                  alert("用户取消了删除");
                }
              },
            },
          });
    </script>
  </body>
</html>
